package Classic150;

import java.util.HashSet;
import java.util.Set;

public class 最长连续序列 {
    /*
        注意增强for循环可以对set使用
     */
    public int longestConsecutive(int[] nums) {
        if (nums.length <= 1) return nums.length;
        int maxLen = 0;
        Set<Integer> set = new HashSet<>();
        for (int num : nums) set.add(num);
        for (int num : set) {
            if (!set.contains(num - 1)) {
                int len = 0;
                while (set.contains(num++)) len++;
                maxLen = Math.max(maxLen, len);
            }
        }
        return maxLen;
    }
}
